package com.gitee.base;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.gitee.context.ApplicationContextHolder;

/**
 * 控制器基类
 * 
 * @author 赵炎
 * @version [V1.00, 2019年1月14日]
 * @since V1.00
 */
public class AbstractController implements InitializingBean
{
    protected static Logger log = LoggerFactory.getLogger(AbstractController.class);
    @Override
    public void afterPropertiesSet() throws Exception
    {
        // 继承类声明字段
        Field[] fields = getClass().getDeclaredFields();
        try
        {
            for (Field field : fields)
            {
                field.setAccessible(true);
                // 字段值
                Object v = field.get(this);
                // 字段类型
                Class<?> clazz = field.getType();
                if (v == null && clazz.getSimpleName().toLowerCase().contains("service"))
                {
                    // 获取Service
                    v = ApplicationContextHolder.getService(clazz);
                    // 注入属性
                    field.set(this, v);
                }
                field.setAccessible(false);
            }
        }
        catch (Exception e)
        {
            log.error("调用service异常", e);
            // afterPropertiesSet();
        }
        
    }
    
}
